/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.anniversary.domain.Alumni;
import me.zhengjie.modules.anniversary.service.AlumniService;
import me.zhengjie.modules.anniversary.service.dto.AlumniQueryCriteria;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @author Jiayin Wei
* @date 2020-07-28
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "杰出校友管理")
@RequestMapping("/api/alumni")
public class AlumniController {

    private final AlumniService alumniService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('alumni:list')")
    public void download(HttpServletResponse response, AlumniQueryCriteria criteria) throws IOException {
        alumniService.download(alumniService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询杰出校友")
    @ApiOperation("查询杰出校友")
    //@PreAuthorize("@el.check('alumni:list')")
    @AnonymousAccess
    public ResponseEntity<Object> query(AlumniQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(alumniService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增杰出校友")
    @ApiOperation("新增杰出校友")
    @PreAuthorize("@el.check('alumni:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Alumni resources){
        return new ResponseEntity<>(alumniService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改杰出校友")
    @ApiOperation("修改杰出校友")
    @PreAuthorize("@el.check('alumni:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Alumni resources){
        alumniService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除杰出校友")
    @ApiOperation("删除杰出校友")
    @PreAuthorize("@el.check('alumni:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        alumniService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("分页查询杰出校友")
    @ApiOperation("分页查询杰出校友")
    @GetMapping("/findByAlumnis")
//@PreAuthorize("@el.check('user:list','dept:list')")
    @AnonymousAccess
    public ResponseEntity<Object> findByAlumnis(Integer page,Integer size) {
        List<Map<String,Object>> alumnis = alumniService.findByAlumnis(PageRequest.of(page, size));
        System.out.println(alumnis);
        return new ResponseEntity<>(alumnis,HttpStatus.OK);
    }
}
